package com.example.q_kang.hhh.order;

import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.order.match.BuyConfirmActivity;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.api.Constant;
import com.sskj.base.bean.BuyDakuanListBean;
import com.sskj.base.mvchelper.ModelRx2DataSource;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author Hey
 * Create at  2019/04/13
 */
public class BuyCompleteFragment extends MVPFragment<BuyCompletePresenter> {

    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private SlimAdapter<BuyDakuanListBean.BuyDakuanBean> slimAdapter;
    private MVCCoolHelper<List<BuyDakuanListBean.BuyDakuanBean>> mvcHelper;

    private SparseArray<CountDownTimer> countDownCounters = new SparseArray<>();
    private Disposable mDisposable;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_buy_complete;
    }

    @Override
    public BuyCompletePresenter getPresenter() {
        return new BuyCompletePresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 20))
                .setDividerColor(ContextCompat.getColor(getContext(), R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_buy_complete, new SlimInjector<BuyDakuanListBean.BuyDakuanBean>() {
            @Override
            public void onInject(BuyDakuanListBean.BuyDakuanBean data, IViewInjector injector) {
                injector.clicked(R.id.tv_check, view -> MatchListActivity.start(getActivity(), Constant.ALL, data.getId(), ""));
                injector.clicked(R.id.tv_follow_order, view -> BuyConfirmActivity.start(getActivity(), data.getId(), "1"))
                        .text(R.id.tv_account, data.getPd_price())
                        .image(R.id.iv_type, data.getOrder_type().equals("1") ? R.mipmap.icon_main :
                                data.getOrder_type().equals("2") ? R.mipmap.icon_fu1 :
                                        data.getOrder_type().equals("3") ? R.mipmap.icon_fu2 :
                                                R.mipmap.icon_fu3)
                        .text(R.id.tv_order, data.getAdd_time());
                TextView tv_count_down = (TextView) injector.findViewById(R.id.tv_count_down);
                CountDownTimer countDownTimer = countDownCounters.get(tv_count_down.hashCode());
                if (countDownTimer != null) {
                    //将复用的倒计时清除
                    countDownTimer.cancel();
                }
                long timer = 0;
                timer = Long.valueOf(data.getDd_time());
                if (timer > 0) {
                    countDownTimer = new CountDownTimer(timer * 1000, 1000) {
                        @Override
                        public void onTick(long l) {
                            tv_count_down.setText(DateUtil.secondToTime(l / 1000));
                        }

                        @Override
                        public void onFinish() {
                            tv_count_down.setText("00:00:00");
                        }
                    }.start();
                    countDownCounters.put(tv_count_down.hashCode(), countDownTimer);
                } else {
                    tv_count_down.setText("00:00:00");
                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<BuyDakuanListBean.BuyDakuanBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<BuyDakuanListBean.BuyDakuanBean>)
                        page -> mPresenter.getBuyComplete(page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (slimAdapter != null) {
            slimAdapter.cancelAllTimers(countDownCounters);
        }
    }

    @Override
    public void initData() {

    }

    public static BuyCompleteFragment newInstance() {
        BuyCompleteFragment fragment = new BuyCompleteFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


}
